package com.lb.statck.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lb.statck.common.result.Result;
import com.lb.statck.mapper.order.OrderMapper;
import com.lb.statck.pojo.dto.codeDto;
import com.lb.statck.pojo.vo.OrderVo;
import com.lb.statck.pojo.vo.codeVo;
import com.lb.statck.pojo.vo.image;
import com.lb.statck.service.OrderService;
import com.lb.statck.service.codeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
 @RestController
@RequestMapping("/api/code")
public class codeController {
    @Autowired
    private codeService codeService;
    @Autowired
    private OrderService   orderService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private com.lb.statck.mapper.image.imageMapper imageMapper;

    @PostMapping("/List")
public Result getCodeList(@RequestBody codeDto dto) {
   // codeService.list();
  Result result = new Result();
  result.setCode(200);
  result.setMsg("success");
  result.setData(codeService.list());
  return result;
}
    @PostMapping("/insertOrder")
public Result insertCode(@RequestBody codeDto dto) {

  String order=  codeService.insertOrder(dto);
    Result result = new Result();
    result.setCode(200);
    result.setData(order);
    result.setMsg("success");
return result;
}

    @PostMapping("/Order")
    public Result getOrder(@RequestBody codeDto dto) {

        String order=  dto.getPayOrder();
        OrderVo order1 = orderMapper.getOrders(order);
        String productCode = order1.getCode();
        QueryWrapper<codeVo> wrapper = new QueryWrapper<>();
        wrapper.eq("product_code",productCode);
        codeVo one = codeService.getOne(wrapper);
        Integer id = one.getId();;
        QueryWrapper<image> imageQueryWrapper = new QueryWrapper<>();
        QueryWrapper<image> codeid = imageQueryWrapper.eq("codeid", id);
        List<String> lists = new ArrayList<>();
        for (image image : imageMapper.selectList(imageQueryWrapper)) {
            lists.add("https://"+image.getImage());
        }
        order1.setUrls(lists);
        //codeService.getOne();
        Result result = new Result();
        result.setCode(200);
        result.setData(order1);
        result.setMsg("success");
        return result;
    }
}
